﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MonoDevelop.Ide.Gui;
using MonoDevelop.Components;
using MonoDevelop.Components.Docking;
using MonoDevelop.Components.Commands;
using MonoDevelop.Core;
using MonoDevelop.Ide;

using Gtk;
using Gdk;


namespace Coq.MonoDevelop
{
    public class SendSelection : CommandHandler
    {
        protected override void Run()
        {
            CoqInteractivePad.CurrentCoqi.SendSelection();
        }
    }

    public class SendLine : CommandHandler
    {
        protected override void Run()
        {
            CoqInteractivePad.CurrentCoqi.SendLine();
        }
    }

    public class CoqInteractivePad : IPadContent
    {
        ConsoleView view = new ConsoleView();
        bool prompting = false;
        string lastCommand = "";
        string currentPath = "";

        public static CoqInteractivePad CurrentCoqi
        {
            get
            {
                return (CoqInteractivePad)CurrentPad.Content;
            }
        }

        public static Pad CurrentPad
        { 
            get {
                    Pad ret = null;
                    try
                    {
                        ret = IdeApp.Workbench.GetPad<CoqInteractivePad>();
                    }
                    catch 
                    { }
                    if (ret != null)
                        return ret;
                    else
                        ret = IdeApp.Workbench.AddPad(new CoqInteractivePad(), "Coq.MonoDevelop.CoqInteractivePad", "Coq Interactive", "Center Bottom", IconId.Null);
                    return ret;
                }  
        }

        InteractiveSession session;
        InteractiveSession Session
        {
            get
            {
                if (session == null)
                    session = setupSession();
                return session;
            }
        }


        public void SendSelection()
        {
            var selection = IdeApp.Workbench.ActiveDocument.TextEditor.SelectedText;
            view.WriteOutput(selection + Environment.NewLine);
            sendCommand(selection);
        }

        public void SendLine()
        {
            var line = IdeApp.Workbench.ActiveDocument.TextEditor.CursorLine;
            var text = IdeApp.Workbench.ActiveDocument.TextEditor.GetLineText(line);
            view.WriteOutput(text + Environment.NewLine);
            sendCommand(text);
        }

        #region Interface
        public Widget Control { get {  return view; } }

        public void Dispose()
        {
        }

        public void Initialize(IPadWindow window)
        {
            view.ConsoleInput += (s, e) => { Session.SendCommand(e.Text); };

            var toolbar = window.GetToolbar(Gtk.PositionType.Right);
            var buttonClear = new DockToolButton("gtk-clear");
            buttonClear.TooltipText = GettextCatalog.GetString("Clear");
            buttonClear.Clicked += (s, e) => { view.Clear(); };

            //var buttonRestart = new DockToolButton("gtk-clear");
            //buttonRestart.TooltipText = "Restart";
            //buttonRestart.Clicked += (s, e) => { RestartCoqi(); };

            toolbar.Add(buttonClear);
            toolbar.ShowAll();
        }

        public void RestartCoqi()
        {
            //Session.Kill();
            //session = null;
        }

        void sendCommand(string str)
        {
            if (str != "")
            {
                Session.SendCommand(str);
                //prompting = false;
            }
        }
    //      and sendCommand (str:string) fromPrompt = 
    //if view <> null then
    //  match !session with
    //  | Some(session) when str <> "" -> 
    //      lastCommand <- str.Trim()
    //      session.SendCommand(str)
    //      prompting <- false
    //  | Some(_) -> ()
    //  | _ -> session := Some(setupSession())

        //void Child_KeyPressEvent(object o, Gtk.KeyPressEventArgs args)
        //{
            
        //}

        public void RedrawContent()
        {
        }

        private InteractiveSession setupSession()
        {
            InteractiveSession ses = new InteractiveSession();
            prompting = true;
            ses.Exited += (e,s) =>
                {
                    MessageBox.Show("oops");
                };

            ses.TextRecieved += 
                (string t) =>
                {
                    view.WriteOutput(t);
                    if (prompting) 
                        view.Prompt(true);
                    if (!prompting)
                    {
                        //int i = 0;
                    }
                };

            ses.PromptReady +=
                () =>
                {
                    if (!prompting)
                    {
                        view.Prompt(true);
                        prompting = true;
                    }
                };
            ses.StartRecieving();
            return ses;
        }
        #endregion 
    }
}
